package com.niiwoo.civet.account.service.local.openAccount2.lanmao;

import com.niiwoo.civet.account.dao.entity.ActivateAccountOrder;
import com.niiwoo.civet.account.service.local.openAccount2.dto.CreateRequest;
import com.niiwoo.civet.account.service.local.openAccount2.type.ActivateTypeable;
import com.niiwoo.tripod.lanmao.component.LanMaoGatewayService;
import com.niiwoo.tripod.lanmao.enums.AuthenticateValidTypeEnum;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import com.niiwoo.tripod.lanmao.request.ActivateAccountRequest;
import com.niiwoo.tripod.lanmao.response.LanMaoGatewayDTO;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivateLanMaoHandler implements OpenAccountLanMaoOperation<ActivateAccountOrder>, ActivateTypeable {

    @Autowired
    LanMaoGatewayService lanMaoGatewayService;

    @Override
    public LanMaoGatewayDTO generateLanMaoGatewayDTO(CreateRequest request, ActivateAccountOrder order) {
        ActivateAccountRequest activateAccountRequest = new ActivateAccountRequest();
        activateAccountRequest.setRequestNo(order.getRequestNo());
        activateAccountRequest.setPlatformUserNo(order.getAccountNo());
        // 仅借款和投资角色开通集团账户
        if (order.getUserRole().equals(UserRoleEnum.BORROWERS.getValue()) || order.getUserRole().equals(UserRoleEnum.INVESTOR.getValue())) {
            activateAccountRequest.setGroupAccount("TRUE");
        }
        activateAccountRequest.setCheckType(AuthenticateValidTypeEnum.LIMIT);
        activateAccountRequest.setTimestamp(new Date());
        activateAccountRequest.setAuthList(order.getAuthList());
        activateAccountRequest.setNiiwooRedirectUrl(request.getRedirectUrl()); // H5重定向地址
        activateAccountRequest.setFailTime(LanMaoHelper.getFixFailTime()); // 授权时间
        activateAccountRequest.setAmount(LanMaoHelper.getFixLimitAmount()); // 授权金额
        return lanMaoGatewayService.activateAccount(activateAccountRequest);
    }

}
